package com.booksystem.controller;

import com.booksystem.common.RestResult;
import com.booksystem.common.ResultCodeConstant;
import com.booksystem.dto.UserInfoUpdateDTO;
import com.booksystem.dto.UserLoginDTO;
import com.booksystem.dto.UserPasswordUpdateDTO;
import com.booksystem.dto.UserRegisterDTO;
import com.booksystem.entity.UserDO;
import com.booksystem.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.groups.Default;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *   用户控制器
 * </p>
 * @author 段伟豪
 */
@Slf4j
@Api("用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册接口
     *
     * @param userRegisterDTO 注册参数对象
     * @return RestResult
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public RestResult<Boolean> register(@RequestBody @Validated(Default.class) UserRegisterDTO userRegisterDTO) {
        boolean result = userService.register(userRegisterDTO);
        if (result) {
            return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
        } else {
            return new RestResult<>(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG, result);
        }
    }

    /**
     * 用户登录接口
     *
     * @param loginDTO 登录参数对象
     * @return RestResult
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public RestResult<UserDO> login(@RequestBody @Validated(Default.class) UserLoginDTO loginDTO) {
        UserDO userDO = userService.login(loginDTO);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, userDO);
    }

    /**
     * 修改个人信息接口
     *
     * @param userInfoUpdateDTO 更新参数对象
     * @return RestResult
     */
    @PutMapping("/info")
    @ApiOperation("修改个人信息")
    public RestResult<Boolean> updateInfo(@RequestBody @Validated(Default.class) UserInfoUpdateDTO userInfoUpdateDTO) {
        boolean result = userService.updateUserInfo(userInfoUpdateDTO);
        if (result) {
            return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
        } else {
            return new RestResult<>(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG, result);
        }
    }

    /**
     * 修改密码接口
     *
     * @param passwordUpdateDTO 密码更新参数对象
     * @return RestResult
     */
    @PutMapping("/password")
    @ApiOperation("修改密码")
    public RestResult<Boolean> updatePassword(@RequestBody @Validated(Default.class) UserPasswordUpdateDTO passwordUpdateDTO) {
        boolean result = userService.updatePassword(passwordUpdateDTO);
        if (result) {
            return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, result);
        } else {
            return new RestResult<>(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG, result);
        }
    }
}